package com.example.demo.mqsever.datacenter;

import com.example.demo.MqApplication;
import com.example.demo.mqsever.core.Binding;
import com.example.demo.mqsever.core.Exchange;
import com.example.demo.mqsever.core.ExchangeType;
import com.example.demo.mqsever.core.MSGQueue;
import com.example.demo.mqsever.mapper.MetaMapper;

import java.io.File;
import java.util.List;

// 整合数据库操作
public class DataBaseManager {
    // 希望从 Spring 中拿到现成的对象
    private MetaMapper  metaMapper;

    // 数据库初始化 = 建库建表 + 插入默认数据
    public void init(){
        // 获取到 MetaMapper（从 Spring 中拿到现成的对象）
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if (!checkDBExists()){
            File dataDir = new File("./data");
            dataDir.mkdirs();
            createTable();
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成");
        } else {
            System.out.println("[DataBaseManager] 数据库已经存在");
        }
    }

    // 删除 meta.db 文件
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret){
            System.out.println("[DataBaseManager] 删除数据库文件成功");
        } else{
            System.out.println("[DataBaseManager] 删除数据库文件失败");
        }


        File fileDir = new File("./data");
        ret = fileDir.delete();
        if (ret){
            System.out.println("[DataBaseManager] 删除数据库目录成功");
        } else{
            System.out.println("[DataBaseManager] 删除数据库目录失败");
        }
    }

    // 执行数据库操作时，会自动创建 mata.db 文件出来（MyBatis）
    private boolean checkDBExists(){
        File file = new File("./data/meta.db");
        if (file.exists()){
            return true;
        } else {
            return false;
        }
    }

    private void createTable(){
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成");
    }

    // 添加默认数据主要是添加一个交换机（匿名 + DIRECT）
    private void createDefaultData(){
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成");
    }

    //封装其他操作
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public List<MSGQueue> selectAllQueue(){
        return metaMapper.selectAllQueue();
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
}
